/**
 * Progetto PRAISED.
 * 24/gen/2011
 */
package it.uniroma3.dia.praised.core.dictionary;


import it.uniroma3.dia.praised.core.model.InputText;
import it.uniroma3.dia.praised.core.model.Token;
import it.uniroma3.dia.praised.core.model.Tokenizer;

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;

/**
 * @author Accosta Augusto
 * @author Lopez Elisabetta
 *
 */
public class ExclusionModule {

	/**
	 * 
	 */
	public ExclusionModule() {}
	
	/**
	 * 
	 * @param inputText
	 * @throws IOException
	 */
	public void excludeTokenFromInput(InputText inputText) throws IOException {
		this.excludeSemanticExpression(inputText);
		List<Token> newTokenList = new LinkedList<Token>();
		newTokenList.addAll(inputText.getTokenList());
		
		for (Token token : inputText.getTokenList()) {
			if(isStopWord(token) || isAminoacid(token) || isUnits(token))
				newTokenList.remove(token);
		}
		
		inputText.setTokenList(newTokenList);
	}
	
	/**
	 * 
	 * @param token
	 * @return
	 * @throws IOException
	 */
	private boolean isStopWord(Token token) throws IOException {
		File stopWordFile = new File("fileData/stopWord.txt");
		List<String> stopWordList = FileUtils.readLines(stopWordFile, "UTF-8");
		
		return stopWordList.contains(token.getToken());
	}
	
	/**
	 * 
	 * @param token
	 * @return
	 * @throws IOException
	 */
	private boolean isUnits(Token token) throws IOException {
		File units = new File("fileData/units.txt");
		List<String> unitsList = FileUtils.readLines(units, "UTF-8");
		
		return unitsList.contains(token.getToken());
	}
	
	/**
	 * 
	 * @param token
	 * @return
	 * @throws IOException
	 */
	private boolean isAminoacid(Token token) throws IOException {
		File aminoacids = new File("fileData/aminoacids.txt");
		List<String> aminoacidsList = FileUtils.readLines(aminoacids, "UTF-8");
		
		for (String aminoacid : aminoacidsList) {
			String regExp = aminoacid + "[0-9]{3}";
			Pattern pattern = Pattern.compile(regExp);
			Matcher matcher = pattern.matcher(token.getToken());
		
			if(matcher.matches())
				return true;
		}
		
		return false;
	}
	
	/**
	 * 
	 * @param inputText
	 * @throws IOException
	 */
	private void excludeSemanticExpression(InputText inputText) throws IOException {
		File semanticExpressions = new File("fileData/semanticExpressions.txt");
		List<String> semanticExpressionsList = FileUtils.readLines(semanticExpressions, "UTF-8");
		
		for (String expression : semanticExpressionsList) {
			String regExp = ".*" + expression + ".*";
			Pattern pattern = Pattern.compile(regExp);
			Matcher matcher = pattern.matcher(inputText.getOriginalTextInput());
		
			if(matcher.matches()) {
				String parsedText = removeDoubleWhiteSpace(inputText.getOriginalTextInput().replace(expression, ""));
				
				inputText.setParsedTextInput(parsedText);
				inputText.setTokenList(Tokenizer.getTokens(parsedText));
				
				break;
			}
		}
	}
	
	/**
	 * 
	 * @param string
	 * @return
	 */
	private static String removeDoubleWhiteSpace(String string) {
		return string.replace("  ", " ");
	}
	
}
